/**
 * @class Aurora.NumberField
 * @extends Aurora.TextField
 * <p>数字输入组件.
 * @author njq.niu@hand-china.com
 * @constructor
 * @param {Object} config 配置对象. 
 * TODO: allowDecimals  allowNegative 这种属性设置到field上,以便行可以动态设置
 */
$S.NumberField = Ext.extend($S.TextField,{
	allowDecimals : true,
    allowNegative : true,
    allowFormat : false,
	baseChars : "0123456789",
    decimalSeparator : ".",
    decimalPrecision : 2,
	constructor: function(config) {
        $S.NumberField.superclass.constructor.call(this, config);
    },
    initComponent : function(config){
        $S.NumberField.superclass.initComponent.call(this, config); 
        this.allowed = this.baseChars+'';
        if(this.allowDecimals){
            this.allowed += this.decimalSeparator;
        }
        if(this.allowNegative){
            this.allowed += "-";
        }
    },
//    initValue : function(v){
//    	this.setRawValue(this.formatValue(v))
////    	if(this.allowFormat){
////            this.setRawValue($S.formatNumber(v));
////    	}
//    },
    initEvents : function(){
    	$S.NumberField.superclass.initEvents.call(this);    	
    },
    onKeyPress : function(e){
        var k = e.getKey();
        if((Ext.isGecko || Ext.isOpera) && (e.isSpecialKey() || k == 8 || k == 46)){//BACKSPACE or DELETE
            return;
        }
        var c = e.getCharCode();
        if(this.allowed.indexOf(String.fromCharCode(c)) === -1){
            e.stopEvent();
            return;
        }
        $S.NumberField.superclass.onKeyPress.call(this, e); 
    },
    formatValue : function(rv){
        var sf = this,rv = sf.fixPrecision(sf.parseValue(rv));
        if(sf.allowFormat)rv = $S.formatNumber(rv);
        return $S.NumberField.superclass.formatValue.call(sf,rv);
    },
    processMaxLength : function(rv){
        var s=rv.split('.'),isNegative=false;
        if(s[0].search(/-/)!=-1)isNegative=true;
        return (isNegative?'-':'')+$S.NumberField.superclass.processMaxLength.call(this, s[0].replace(/[-,]/g,''))+(s[1]?'.'+s[1]:''); 
    },
    processValue : function(v){
    	return this.parseValue(v);
    },
    onFocus : function(e) {
    	if(this.allowFormat&&!this.disabled) {
            this.setRawValue($S.removeNumberFormat(this.getRawValue()));
        }
    	$S.NumberField.superclass.onFocus.call(this,e);
    },
    parseValue : function(v){
        var sf = this;
        v = String(v);
        if(v.indexOf(",")!=-1)v=v.replace(/,/g,"");
    	if(!sf.allowNegative)v = v.replace('-','');
    	if(!sf.allowDecimals)v = v.indexOf(".")==-1?v:v.substring(0,v.indexOf("."));
        v = parseFloat(sf.fixPrecision(v.replace(this.decimalSeparator, ".")));
        return isNaN(v) ? '' : v;
    },
    fixPrecision : function(v){
        var nan = isNaN(v);
        if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !v){
           return nan ? '' : v;
        }
        return parseFloat(v).toFixed(this.decimalPrecision);
    }
})